/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dorian.atomicuml.dao;

import com.dorian.atomicuml.dao.interfaces.UsersProjectManager;
import com.dorian.atomicuml.orm.UsersProjects;
import com.dorian.atomicuml.orm.Projects;
import com.dorian.atomicuml.orm.Users;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author Ayden
 */
public class UsersProjectManagerImpl implements UsersProjectManager {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public boolean addUser(String projectTitle, String email) {
        Session session = sessionFactory.getCurrentSession();
        Query findUser = session.getNamedQuery("USERS.findByMail").setParameter("mail", email);
        Query findProject = session.getNamedQuery("Projects.findByTitle").setParameter("title", projectTitle);
        Projects p = (Projects) findProject.uniqueResult();
        Users u = (Users) findUser.uniqueResult();
        if (!(u != null && p != null)) {
            return false;
        }

        if (!isUsersProject(p.getId(), u.getId())) {
            UsersProjects up = new UsersProjects(0, u.getId(), p.getId());
            session.save(up);
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteUsersProjects(int projectId, int usersId) {
        Session session = sessionFactory.getCurrentSession();

        try {
            Query findUP = session.getNamedQuery("UsersProjects.findByProjectsIdAndUsersId")
                                  .setParameter("projects_id", projectId)
                                  .setParameter("users_id", usersId);
            session.delete(findUP.uniqueResult());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteUsersProjects(UsersProjects up) {
        Session session = sessionFactory.getCurrentSession();
        try {
            session.delete(up);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<UsersProjects> getUsersProject(int projectId) {
        Session session = sessionFactory.getCurrentSession();
        Query users = session.createSQLQuery("SELECT * from users_projects where projects_id = :projects_id")
                             .addEntity(UsersProjects.class)
                             .setParameter("projects_id", projectId);
        return users.list();
    }

    @Override
    public boolean isUsersProject(int projectId, int userId) {
        Session session = sessionFactory.getCurrentSession();
        Query findUP = session.getNamedQuery("UsersProjects.findByProjectsIdAndUsersId")
                              .setParameter("projects_id", projectId)
                              .setParameter("users_id", userId);
        return (findUP.uniqueResult() != null);
    }

    @Override
    public List<Projects> getAllProjectsFromUsersID(int id) {
        Session session = sessionFactory.getCurrentSession();
        List<Projects> projectList = null;

        try {
            Query findProjects = session.createSQLQuery("select * from USERS_PROJECTS up where up.users_id = :users_id")
                                        .addEntity(UsersProjects.class)
                                        .setParameter("users_id", id);
            List<UsersProjects> list = findProjects.list();

            projectList = new ArrayList<Projects>();
            Query findProject = session.getNamedQuery("Projects.findById");
            for (UsersProjects up : list) {
                findProject.setParameter("id", up.getProjectsId());
                projectList.add((Projects) findProject.uniqueResult());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return projectList;
    }
}
